<?php namespace App\Utils;

use Exception;
use Oss\OssClient;

class OssTool{
    public static $bucket;
    public static function newOss(){
        $accessKeyId=env('OSS_ACCESSKEYID');
        $accessKeySecret=env('OSS_ACCESSKEYSECRET');
        $endpoint=env('OSS_ENDPOINT');
        self::$bucket=env('OSS_BUCKET');
        return new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    }

    public static function uploadFile($filePath,$fileName) {
        if($fileName && $filePath) {
            $ossClient = self::newOss();
            $ossClient->uploadFile(self::$bucket, $fileName, $filePath);
            return true;
        } else {
            return false;
        }
    }

    public static function delFile($fileName) {
        if($fileName) {
            $ossClient = self::newOss();
            $ossClient->deleteObject(self::$bucket, $fileName);
            return true;
        } else {
            return false;
        }
    }

    public static function cutFile($fromObject,$toObject) {
        if($fromObject && $toObject) {
            $ossClient = self::newOss();
            $ossClient->copyObject(self::$bucket, $fromObject,self::$bucket,$toObject);
            $ossClient->deleteObject(self::$bucket, $fromObject);
            return true;
        } else {
            return false;
        }
    }

    public static function formatBizQueryParaMap($paraMap, $urlEncode) {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if($urlEncode) {
                $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar='';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }

    public static function getStsAuth() {
        try {
            $data['Version'] = '2015-04-01';
            $data['AccessKeyId'] = 'LTAI1OkLUvPVWRFA';
            $data['SignatureMethod'] = 'HMAC-SHA1';
            $data['SignatureVersion'] = '1.0';
            $data['SignatureNonce'] = rand('100000', '999999');
            $data['Timestamp'] = date('Y-m-d\TH:i:s\Z', time() - 28800);
            //$data['Timestamp'] = date('Y-m-d\TH:i:s\Z',time()-18600);
            $data['Format'] = 'json';
            $data['Action'] = 'AssumeRole';
            $data['RoleArn'] = 'acs:ram::1559363382399075:role/fbwosstemp';
            $data['RoleSessionName'] = 'fbw';
            $data['Signature'] = base64_encode(hash_hmac('sha1', 'GET&%2F&' . urlencode(self::formatBizQueryParaMap($data, true)), 'm2y2Ic96EiSaTBAxEF7cPeLnXqIG9A&', true));
            $bizString = self::formatBizQueryParaMap($data, true);
            $url = 'https://sts.aliyuncs.com?' . $bizString;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            $res = curl_exec($ch);
            curl_close($ch);
            $res = json_decode($res, true);
            return $res['Credentials'];
        } catch(\Exception $e) {
            return false;
        }
    }
}

?>